using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using JlPay.Sdk.Models;
using JlPay.Sdk.Services;

namespace JlPay.Sdk.Models.Openmerch.Register
{
    /// <summary>
    /// 进件查询响应
    /// </summary>
    public class IncomingQueryResponse : StandardResponse, IComplexFieldHandler
    {
        /// <summary>
        /// 业务申请编号
        /// </summary>
        [JsonPropertyName("apply_id")]
        public string? ApplyId { get; set; }

        /// <summary>
        /// 审核状态
        /// </summary>
        [JsonPropertyName("audit_status")]
        public string? AuditStatus { get; set; }

        /// <summary>
        /// 审核驳回详情
        /// </summary>
        [JsonPropertyName("audit_detail")]
        public List<AuditDetail>? AuditDetail { get; set; }

        /// <summary>
        /// 审核失败原因
        /// </summary>
        [JsonPropertyName("audit_failure")]
        public string? AuditFailure { get; set; }

        /// <summary>
        /// 商户号
        /// </summary>
        [JsonPropertyName("merch_no")]
        public string? MerchNo { get; set; }

        /// <summary>
        /// 协议编号
        /// </summary>
        [JsonPropertyName("sign_id")]
        public string? SignId { get; set; }

        /// <summary>
        /// 协议状态
        /// </summary>
        [JsonPropertyName("sign_status")]
        public string? SignStatus { get; set; }

        /// <summary>
        /// 协议链接
        /// </summary>
        [JsonPropertyName("sign_url")]
        public string? SignUrl { get; set; }

        /// <summary>
        /// 受托人协议编号
        /// </summary>
        [JsonPropertyName("sign_delegatee_id")]
        public string? SignDelegateeId { get; set; }

        /// <summary>
        /// 受托人协议状态
        /// </summary>
        [JsonPropertyName("sign_delegatee_status")]
        public string? SignDelegateeStatus { get; set; }

        /// <summary>
        /// 受托人协议链接
        /// </summary>
        [JsonPropertyName("sign_delegatee_url")]
        public string? SignDelegateeUrl { get; set; }

        /// <summary>
        /// 终端号
        /// </summary>
        [JsonPropertyName("term_no")]
        public string? TermNo { get; set; }

        /// <summary>
        /// 审核结束时间
        /// </summary>
        [JsonPropertyName("audit_end_time")]
        public string? AuditEndTime { get; set; }

        /// <summary>
        /// 银联报备结果
        /// </summary>
        [JsonPropertyName("unionpay_report_ret")]
        public string? UnionpayReportRet { get; set; }

        /// <summary>
        /// 银联报备消息
        /// </summary>
        [JsonPropertyName("unionpay_report_msg")]
        public string? UnionpayReportMsg { get; set; }

        /// <summary>
        /// 微信报备结果
        /// </summary>
        [JsonPropertyName("wechat_report_ret")]
        public string? WechatReportRet { get; set; }

        /// <summary>
        /// 微信报备消息
        /// </summary>
        [JsonPropertyName("wechat_report_msg")]
        public string? WechatReportMsg { get; set; }

        /// <summary>
        /// 支付宝报备结果
        /// </summary>
        [JsonPropertyName("alipay_report_ret")]
        public string? AlipayReportRet { get; set; }

        /// <summary>
        /// 支付宝报备消息
        /// </summary>
        [JsonPropertyName("alipay_report_msg")]
        public string? AlipayReportMsg { get; set; }

        /// <summary>
        /// 子应用绑定结果
        /// </summary>
        [JsonPropertyName("sub_appid_bind_ret")]
        public string? SubAppidBindRet { get; set; }

        /// <summary>
        /// 子应用绑定消息
        /// </summary>
        [JsonPropertyName("sub_appid_bind_msg")]
        public string? SubAppidBindMsg { get; set; }

        /// <summary>
        /// 功能列表
        /// </summary>
        [JsonPropertyName("functions")]
        public List<Dictionary<string, object>>? Functions { get; set; }

        /// <summary>
        /// 处理复杂字段
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="fieldValue">字段值</param>
        public void HandleComplexField(string fieldName, object fieldValue)
        {
            if (fieldName == "audit_detail")
            {
                if (fieldValue is JsonElement jsonElement)
                {
                    if (jsonElement.ValueKind == JsonValueKind.Array)
                    {
                        AuditDetail = new List<AuditDetail>();
                        foreach (var item in jsonElement.EnumerateArray())
                        {
                            if (item.ValueKind == JsonValueKind.Object)
                            {
                                var auditDetail = JsonSerializer.Deserialize<AuditDetail>(item.GetRawText(), new JsonSerializerOptions 
                                { 
                                    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower 
                                });
                                if (auditDetail != null)
                                {
                                    AuditDetail.Add(auditDetail);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 审核详情
    /// </summary>
    public class AuditDetail
    {
        /// <summary>
        /// 模块变量名
        /// </summary>
        [JsonPropertyName("module")]
        public string? Module { get; set; }

        /// <summary>
        /// 模块中文名
        /// </summary>
        [JsonPropertyName("module_name")]
        public string? ModuleName { get; set; }

        /// <summary>
        /// 模块审核不通过原因
        /// </summary>
        [JsonPropertyName("failed_reason")]
        public string? FailedReason { get; set; }

        /// <summary>
        /// 字段审核不通过原因列表
        /// </summary>
        [JsonPropertyName("module_detail")]
        public List<FieldDetail>? ModuleDetail { get; set; }
    }

    /// <summary>
    /// 字段详情
    /// </summary>
    public class FieldDetail
    {
        /// <summary>
        /// 字段名
        /// </summary>
        [JsonPropertyName("field_name")]
        public string? FieldName { get; set; }

        /// <summary>
        /// 字段中文名
        /// </summary>
        [JsonPropertyName("field_name_cn")]
        public string? FieldNameCn { get; set; }

        /// <summary>
        /// 字段值
        /// </summary>
        [JsonPropertyName("field_value")]
        public string? FieldValue { get; set; }

        /// <summary>
        /// 审核不通过原因
        /// </summary>
        [JsonPropertyName("failed_reason")]
        public string? FailedReason { get; set; }
    }
} 